
import { _decorator, Component, Node, Label } from 'cc';
const { ccclass, property } = _decorator;

import * as i18n from 'db://i18n/LanguageData';

@ccclass('LanguageMenu')
export class LanguageMenu extends Component {


    @property(Label)
    dropdownLabel: Label= null!;
    
    @property(Node)
    DropdownContent: Node= null!;

    onLoad()
    {
        let u= "en";
        try {
            var json= window["GetHtmlLanguage"]();
            u=json["language"];
            //u=u.toUpperCase();
            console.log("Language:"+u);
        } catch (error) {
            
        }

        switch (u) {
            case "en":
                this.dropdownLabel.string = "Enlish";
                i18n.init('en');
                break;
            case "zhCN":
                this.dropdownLabel.string = "中文";
                i18n.init('zhCN');
                break;
            case "ja":
                this.dropdownLabel.string = "日本語";
                i18n.init('ja');
                break;
        }
        i18n.updateSceneRenderers();
    }

    start() {

    }

    ClickDropDownBtn()
    {
        if (this.DropdownContent.active) {
            this.DropdownContent.active=false;
        }else
        {
            this.DropdownContent.active=true;
        }
    }

    onClickEnlish() 
    {
        this.changeLanguage("en");
    }
    onClickChina() 
    {
        this.changeLanguage("zhCN");

    }
    onClickJa() 
    {
        this.changeLanguage("ja");
    }

    changeLanguage(language) {

        switch (language) {
            case "en":
                this.dropdownLabel.string = "Enlish";
                i18n.init('en');
                break;
            case "zhCN":
                this.dropdownLabel.string = "中文";
                i18n.init('zhCN');
                break;
            case "ja":
                this.dropdownLabel.string = "日本語";
                i18n.init('ja');
                break;
        }

        i18n.updateSceneRenderers();
        this.ClickDropDownBtn();
    }
}


